function [ nlines topRow bottomRow leftColumn rightColumn ] = lineBoundaries( img )
    %Extracts the boundary of the all lines given a start row
    %Given image should be binary
    
    %Start from the startRow. advance row by row to until finding an empty
    %line
    
    %Dummy initialization
    nlines = 0;
    topRow = 0;
    bottomRow = 0;
    leftColumn = 0;
    rightColumn = 0;
    
    searchStartLine = 1;
    nrows = size(img,1);
    
    while 1
        %Start text row is the first row we hit which includes a 0 (black 
        %pixel) 
        
        textRows = find(any(img(searchStartLine:nrows,:) == 0,2));
        if isempty(textRows)
            return;
        end
        %Increment the number of text lines we have found
        nlines = nlines + 1;
        
        topRow(nlines) = searchStartLine + min(textRows) - 1;
        %Background row start is the first row with completely 1s (white
        %pixels)
        backgroundRows = find(all(img(topRow(nlines)+1:nrows,:) == 1, 2));
        %We are at the bottom line and also there is no background lines
        %following
        if isempty(backgroundRows)
            backgroundRows = nrows+1;
        end
        bottomRow(nlines) = topRow(nlines) + min(backgroundRows) - 1;
        %Now extract the column boundaries of the current line
        leftColumn(nlines) = min(find(any(img(topRow(nlines):bottomRow(nlines),:)==0,1)));
        rightColumn(nlines) = max(find(any(img(topRow(nlines):bottomRow(nlines),:)==0,1)));
        
        %Increment the search start line to bottom of the current line + 1
        searchStartLine = bottomRow(nlines) + 1;
        if searchStartLine >= nrows
            return
        end
        
    end
end

